package com.kjsdfgk.hjmdsf.login.mvp.presenter;

import android.app.Application;

import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.FileIOUtils;
import com.blankj.utilcode.util.SPUtils;
import com.jess.arms.base.App;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import javax.inject.Inject;

import com.jess.arms.utils.RxLifecycleUtils;
import com.kjsdfgk.hjmdsf.app.entity.AccountJson;
import com.kjsdfgk.hjmdsf.app.msg.AddViewMsg;
import com.kjsdfgk.hjmdsf.login.mvp.contract.LoginContract;

import org.greenrobot.eventbus.EventBus;

import java.io.File;

import static com.kjsdfgk.hjmdsf.app.Config.DATA_PATH;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 04/19/2020 00:43
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class LoginPresenter extends BasePresenter<LoginContract.Model, LoginContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public LoginPresenter(LoginContract.Model model, LoginContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

    public void userLogon(String account, String password)  {
        try {



            SPUtils.getInstance().put("account",account);
            SPUtils.getInstance().put("password",password);
            mModel.userLogon(account , password )
                    .subscribeOn(Schedulers.io())
                    .subscribeOn(AndroidSchedulers.mainThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                    .subscribe(new ErrorHandleSubscriber<AccountJson>(mErrorHandler) {
                        @Override
                        public void onError(@NonNull Throwable e) {

                            mRootView.hideLoading();
                            mRootView.showMessage("账号密码不正确");

                        }

                        @Override
                        public void onNext(@NonNull AccountJson accountJson) {
                            if (accountJson.getCode() == 200){
                                mRootView.hideLoading();
                                ((App) mApplication).getAppComponent().extras().put("Account", accountJson.getMsg().getInfo());
                                accountJson.getMsg().getInfo().setToken(accountJson.getMsg().getToken());
                                mRootView.showAccount( accountJson.getMsg().getInfo());
                                EventBus.getDefault().post(new AddViewMsg("getUserInfo",""));
                            }else {
                                mRootView.showMessage(accountJson.getMsg().toString());
                            }


                        }
                    });
        }catch (Exception e){
            mRootView.showMessage(e.toString());
        }

    }
}
